473,465 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

question about .. my 'unpacker'.


I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I'd like a - sort of - generic approach that'll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.

Sep 11 '05 #1
10 1612
ma******@pegasus.cc.ucf.edu wrote:
....

I'd like a - sort of - generic approach that'll acount for floats and
doubles.
Somthing like this ?

template < typename InT, typename OutT, unsigned N >
void unpack(
InT beg,
InT end,
OutT ( & dest )[ N ]
)
{
const unsigned int mask=(1<<20)-1;

unsigned i = 0;

while (beg!=end)
{
dest[ i++ ] = *beg & mask;
if ( i >= N ) throw Overflow;

dest[ i++ ] = *beg >> 20 & ~mask;
if ( i >= N ) throw Overflow;
++beg;
}
}

I suspect I should also use a vector, nonetheless critiques and/or a more refined implementation welcome.

Sep 12 '05 #2
* ma******@pegasus.cc.ucf.edu:

I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
'const' for both those arguments.

float* dest)
mysterious choice of result type.

{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
Here you're copying the lower 20 bits.

*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 12 '05 #3
ma******@pegasus.cc.ucf.edu wrote:
I'm trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don't cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
This certainly won't work. For one, it only gives you two values, not
three.
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I'd like a - sort of - generic approach that'll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.


#include <vector>
using namespace std;

typedef unsigned int uint;

template <typename DestType>
void unpack( vector<uint>::const_iterator srcBegin,
vector<uint>::const_iterator const srcEnd,
vector<DestType>::iterator dest )
{
static const uint mask = (1<<10)-1;
while( srcBegin++ != srcEnd )
{
*dest++ = DestType( (*srcBegin ) & mask );
*dest++ = DestType( (*srcBegin >> 10) & mask );
*dest++ = DestType( (*srcBegin >> 20) & mask );
}
}

Cheers! --M

Sep 12 '05 #4
mlimber wrote:
....
I'd like a - sort of - generic approach that'll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.

#include <vector>
using namespace std;

typedef unsigned int uint;

template <typename DestType>
void unpack( vector<uint>::const_iterator srcBegin,
vector<uint>::const_iterator const srcEnd,
vector<DestType>::iterator dest )
{
static const uint mask = (1<<10)-1;
while( srcBegin++ != srcEnd )
{
*dest++ = DestType( (*srcBegin ) & mask );
*dest++ = DestType( (*srcBegin >> 10) & mask );
*dest++ = DestType( (*srcBegin >> 20) & mask );
}
}


Good catch on the algorithm issues. On the templateization however,
unless you really want to hold down the parameters to be vector
iterators, you can simply let the compiler figure out the template
argument. If you do this carefully enough, your template code will be
more generic, for example, the code below works for both vectors or
plain arrays (or any combination of them). Hence, the same unpack() code
can be used generically in legacy code as well as new code that uses vector.
template <typename SrcType, typename DestType, typename DestValueType>
void unpack_helper(
SrcType srcBegin,
SrcType srcEnd,
DestType dest,
const DestValueType & /*dest_value unused*/
) {
static const unsigned mask = (1<<10)-1;
unsigned int i = 0;
while( srcBegin++ != srcEnd )
{
dest[ i ++ ] = DestValueType( (*srcBegin ) & mask );
dest[ i ++ ] = DestValueType( (*srcBegin >> 10) & mask );
dest[ i ++ ] = DestValueType( (*srcBegin >> 20) & mask );
}
}

template <typename SrcType1, typename SrcType2, typename DestType>
void unpack( SrcType1 srcBegin,
SrcType2 srcEnd,
DestType dest
) {
unpack_helper< SrcType2, DestType >(
srcBegin, srcEnd, dest, dest[ 0 ]
);
}
void fp()
{
int x[1] = {0xaaaaaa};

float v[3];

unpack( x, x+1, v );

}

#include <vector>
void fv()
{
std::vector<int> x(1);

x[1] = 0xaaaaaa;

std::vector<double> v(3);

unpack( x.begin(), x.end(), v );

}

int main()
{

fp();
fv();
}

Cheers! --M

Sep 12 '05 #5

Gotta love comp.lang.c++. You guys are good. Thanks all. Nothing
like seeing the implementations, more specifically the different
implementation approaches. A humbling experience since it highlights
how far I have to go. Makes me think I should have been a comp sci
major as opposed to EE-DSP. :)

Sep 12 '05 #6
Gianni Mariani wrote:
Good catch on the algorithm issues. On the templateization however,
unless you really want to hold down the parameters to be vector
iterators, you can simply let the compiler figure out the template
argument. If you do this carefully enough, your template code will be
more generic, for example, the code below works for both vectors or
plain arrays (or any combination of them). Hence, the same unpack() code
can be used generically in legacy code as well as new code that uses vector.

[snip]

Good points. I was assuming that the OP wants to restrict the incoming
data to unsigned int because of the specified hardware packing scheme.
Your scheme does make it more general but also allows the user to
introduce potentially dangerous code. Perhaps a via media can be found
with dumb pointers (allowing more abuse but also providing greater
flexibility than my previous post and allowing less abuse but also
providing less flexibility than your post):

template<typename DestType>
void Unpack( uint const* const begin,
uint const* const end,
DestType * const dest )
{
// Same as above
}

void Foo()
{
const uint n = 0xC0FFEE;
vector<float> dest( 3 );
Unpack( &n, &n+1, &dest[0] );
// ...
}

void Bar()
{
const vector<uint> n;
n.push_back( 0xC0FFEE );
float dest[ 3 ];
Unpack( &n[ 0 ], &n[ n.size() ], dest );
// ...
}

Presumably the OP's actual circumstances will determine what approach
is best.

Cheers! --M

Sep 12 '05 #7
ma******@pegasus.cc.ucf.edu wrote:
Gotta love comp.lang.c++. You guys are good. Thanks all. Nothing
like seeing the implementations, more specifically the different
implementation approaches. A humbling experience since it highlights
how far I have to go. Makes me think I should have been a comp sci
major as opposed to EE-DSP. :)


I'm an EE.
Sep 12 '05 #8
Gianni Mariani wrote:
....

#include <vector>
void fv()
{
std::vector<int> x(1);

x[1] = 0xaaaaaa;
should be :
x[0] = 0xaaaaaa;

std::vector<double> v(3);

unpack( x.begin(), x.end(), v );

}


:-(
Sep 12 '05 #9
mlimber wrote:
....

Good points. I was assuming that the OP wants to restrict the incoming
data to unsigned int because of the specified hardware packing scheme.
Your scheme does make it more general but also allows the user to
introduce potentially dangerous code. Perhaps a via media can be found
with dumb pointers (allowing more abuse but also providing greater
flexibility than my previous post and allowing less abuse but also
providing less flexibility than your post):
Yep, good points also.

Presumably the OP's actual circumstances will determine what approach
is best.


As always.

G
Sep 12 '05 #10

Gianni Mariani wrote:
ma******@pegasus.cc.ucf.edu wrote:
Makes me think I should have been a comp sci
major as opposed to EE-DSP. :)


I'm an EE.


Me too, also with a specialization in DSP. I'm currently writing code
for dual TI DSPs in an embedded system (TI has a very conformant
compiler, possibly the EDG front end, but no STL; I have my required
parts of STLport working, though).

Reading some good books on C++ and practice can really bridge the gap,
ma740988. Don't give up, and don't change majors!

Cheers! --M

Sep 12 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: ma740988 | last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples. Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and an inverse FFT, I cast the 10 bit values into doubles....
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
1
by: liming | last post by:
I have installed postgresql 8.3.1,import data from a dB to another one.I work in pgadmin 3 . When I choose a table ,select the characteristic?it shows the problem below? Error:relation...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.